# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=globjects
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0.r178.g4363356a
pkgrel=1
_commit=4363356ae2ef5c936ab078fba48d1ea507c295c4
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://github.com/cginternals/globjects'
pkgdesc="Cross-platform C++ wrapper for OpenGL API objects. (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glbinding"
         "${MINGW_PACKAGE_PREFIX}-glm")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "git")
license=('MIT')
options=('strip' 'staticlibs' 'docs')
source=(${_realname}::"git+https://github.com/cginternals/globjects.git#commit=$_commit"
        'mingw-unix-layout.patch'
        'config-export-location.patch'
        '001-fix-linking-to-glm.patch'
        'globjects.patch')
sha256sums=('SKIP'
            '193434a6351f71926998813afa1830812f666eff6f749e6e640fa3f4ce0d6d28'
            'b1d176d7788d973fe19a13f1d6524b75058fa8cdd66b4a65c4811feef5978ac9'
            'ee24969a0b74dedc7995d8e8ae417bc142a6df7edf8841413a0f46ace940e863'
            'a87527ed097239d3fa0a7e533b63a4b642a39d6434fe977d2ed8f1cbafc14bb8')

pkgver() {
  cd "${srcdir}/${_realname}"
  git describe --long --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/${_realname}"
  # sed -i 's/$/\r/' "${srcdir}/mingw-unix-layout.patch"
  patch -p1 -i "${srcdir}/mingw-unix-layout.patch"
  patch -p1 -i "${srcdir}/config-export-location.patch"
  patch -p1 -i "${srcdir}/001-fix-linking-to-glm.patch"
  # https://aur.archlinux.org/cgit/aur.git/plain/globjects.patch?h=globjects-git
  patch -p1 -i "${srcdir}/globjects.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DOPTION_BUILD_EXAMPLES=OFF \
    -DSYSTEM_DIR_INSTALL=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DOPTION_BUILD_DOCS=ON \
    ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .
}
